package youcaihuaoa.weetop.com.vm.main

import android.app.Application
import androidx.databinding.ObservableField
import api.UserServiceFactory
import com.robot.base.base.vm.GLBaseViewModel
import com.robot.base.common.api.AbstractViewModelSubscriber
import com.robot.base.configs.AppConstants
import com.robot.base.configs.PrefsManager
import com.robot.base.model.UserInfoBean
import com.robot.base.util.arouter.ARouterUtil
import io.reactivex.Observable
import me.goldze.mvvmhabit.binding.command.BindingAction
import me.goldze.mvvmhabit.binding.command.BindingCommand
import me.goldze.mvvmhabit.http.BaseResponse

class MyFragmentViewModel(application: Application) : GLBaseViewModel(application) {

    val userName = ObservableField("")
    val headUrl = ObservableField<String>()
    val phone = ObservableField<String>()
    val jobName = ObservableField<String>()

    fun initData() {
        userName.set(PrefsManager.getUserInfo().user_name)
        headUrl.set(PrefsManager.getUserInfo().head_img)
        phone.set(PrefsManager.getUserInfo().phone)
        jobName.set("${PrefsManager.getUserInfo().department_name}-${PrefsManager.getUserInfo().job_name}")
    }
    val toSignCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivity(AppConstants.Router.Main.A_SIGN)
    })
    val clickJumpNotifyPageCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivity(AppConstants.Router.Main.A_NOTIFY_MESSAGE)
    })

    val clickJumpWagesPageCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivity(AppConstants.Router.Main.A_WAGES)
    })

    val clickJumpUserProfilePageCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivity(AppConstants.Router.Main.A_BASIC_INFO)
    })

    val clickJumpMyExamineListCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivity(AppConstants.Router.Main.A_MY_EXAMINE_LIST)
    })

    val clickJumpMyApplyListCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivity(AppConstants.Router.Main.A_MY_APPLY_LIST)
    })

    fun getUser() {
        UserServiceFactory.getUser()
            .subscribe(object : AbstractViewModelSubscriber<BaseResponse<UserInfoBean>>(this) {
                override fun onNext(t: BaseResponse<UserInfoBean>) {
                    PrefsManager.saveUserInfo(t.data)
                    ARouterUtil.goToActivity(AppConstants.Router.Main.A_MAIN)
                }
            })
    }
}